﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net

Module InternetSpeed
    Function InternetSpeed()

        ' the URL to download a file from
        Dim URL As New Uri("http://speedtest.meamod.com/1024.stf")
        Dim wc As New WebClient()

        ' get current tickcount 
        Dim starttime As Double = Environment.TickCount

        ' download file from the specified URL, and save it to C:\speedtest.txt
        wc.DownloadData(URL)

        ' get current tickcount
        Dim endtime As Double = Environment.TickCount

        ' how many seconds did it take?
        ' we are calculating this by subtracting starttime from endtime
        ' and dividing by 1000 (since the tickcount is in miliseconds.. 1000 ms = 1 sec)
        Dim secs As Double = Math.Floor(endtime - starttime) / 1000

        ' round the number of secs and remove the decimal point
        Dim secs2 As Double = Math.Round(secs, 0)

        ' calculate download rate in kb per sec.
        ' this is done by dividing 1024 by the number of seconds it
        ' took to download the file (1024 bytes = 1 kilobyte)
        Dim kbsec As Double = Math.Round(1024 / secs)

        InternetSpeed = ("1mb download: " & Chr(9) & secs2 & " secs (" & secs & "secs)" & vbNewLine & "Download rate: " & Chr(9) & kbsec & " kb/sec")
    End Function
End Module
